/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.U.
 */
package es.silenus.detecta.batch.tasklet;

import es.silenus.detecta.dao.ImportedFileDAO;

import org.springframework.batch.core.*;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;

import org.springframework.batch.item.ExecutionContext;

import org.springframework.batch.repeat.RepeatStatus;

import java.sql.Date;


/**
 * Import file tasklet.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 24-ago-2009 16:23:02
 */
public class SetupImportFileTasklet implements Tasklet, StepExecutionListener {
	/**
	 * The step execution.
	 */
	private StepExecution stepExecution;

	/**
	 * The imported file DAO.
	 */
	private ImportedFileDAO importedFileDAO;

	/**
	 * Set the imported file DAO.
	 *
	 * @param importedFileDAO the imported file DAO.
	 */
	public void setImportedFileDAO(final ImportedFileDAO importedFileDAO) {
		this.importedFileDAO = importedFileDAO;
	}

	/**
	 * Given the current context in the form of a step contribution, do whatever is necessary to process this unit
	 * inside a transaction. Implementations return {@link RepeatStatus#FINISHED} if finished. If not they return {@link
	 * RepeatStatus#CONTINUABLE}. On failure throws an exception.
	 *
	 * @param contribution mutable state to be passed back to update the current step execution
	 * @param chunkContext attributes shared between invocations but not between restarts
	 *
	 * @return an {@link RepeatStatus} indicating whether processing is continuable.
	 *
	 * @throws Exception if something goes wrong.
	 */
	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
		final Date startDate = new Date(System.currentTimeMillis());

		JobParameters parameters = stepExecution.getJobExecution().getJobInstance().getJobParameters();

		ExecutionContext context = stepExecution.getExecutionContext();

		context.put("importDate", startDate);
		context.put("importedFile", importedFileDAO.findById(parameters.getLong("importedFileId")));

		return RepeatStatus.FINISHED;
	}

	/**
	 * Initialize the state of the listener with the {@link StepExecution} from the current scope.
	 *
	 * @param stepExecution the step execution.
	 */
	public void beforeStep(final StepExecution stepExecution) {
		this.stepExecution = stepExecution;
	}

	/**
	 * Give a listener a chance to modify the exit status from a step. The value returned will be combined with the
	 * normal exit status using {@link ExitStatus#and(ExitStatus)}. Called after execution of step's processing logic
	 * (both successful or failed). Throwing exception in this method has no effect, it will only be logged.
	 *
	 * @param stepExecution the step execution.
	 *
	 * @return an {@link ExitStatus} to combine with the normal value. Return null to leave the old value unchanged.
	 */
	public ExitStatus afterStep(StepExecution stepExecution) {
		return null;
	}
}
